﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RacePlatformer.Object;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using RacePlatformer;

namespace Dwarfs.Manager
{
    public class PlayerManager
    {
        public const float movementSpeed = 5;
        public const float splitSpeed = 10;
        public const float jumpPower = -10;
        public static List<BasePlayer> players = new List<BasePlayer>();
        public static List<BasePlayer> playerGroupA = new List<BasePlayer>();
        public static List<BasePlayer> playerGroupB = new List<BasePlayer>();
        private KeyboardState previouskeyboardstate;
        public bool splitting = false;
        public bool hasControl = false;
        public bool playerDied = false;
        private MainGame maingame;
        
        public PlayerManager(MainGame maingame)
        {
            this.maingame = maingame;
        }
        public static void initialize()
        {
            players = new List<BasePlayer>();
            playerGroupA = new List<BasePlayer>();
            playerGroupB = new List<BasePlayer>();
        }
        public void update(GameTime gametime)
        {
            if (hasControl && !playerDied)
            {
                KeyboardState currentKeyBoardState = Keyboard.GetState();
                if (currentKeyBoardState.IsKeyDown(Keys.Space))
                {
                    if (players.Count > 1)
                    {
                        splitting = true;
                        {
                            foreach (BasePlayer player in playerGroupB)
                            {
                                player.desiredSpriteEffect = Microsoft.Xna.Framework.Graphics.SpriteEffects.None;
                                player.feetBody.LinearVelocity = new Vector2(splitSpeed, player.feetBody.LinearVelocity.Y);
                            }
                            foreach (BasePlayer player in playerGroupA)
                            {
                                player.desiredSpriteEffect = Microsoft.Xna.Framework.Graphics.SpriteEffects.FlipHorizontally;
                                player.feetBody.LinearVelocity = new Vector2(-splitSpeed, player.feetBody.LinearVelocity.Y);
                            }
                        }
                    }
                }
                if (!currentKeyBoardState.IsKeyDown(Keys.Space))
                {
                    if (players.Count > 1)
                    //only allow splitting if more than 1 player is in the game
                    {
                        if (Math.Abs(playerGroupA[playerGroupA.Count - 1].body.Position.X - playerGroupB[0].body.Position.X) > 2f)
                        {
                            foreach (BasePlayer player in playerGroupB)
                            {
                                player.desiredSpriteEffect = Microsoft.Xna.Framework.Graphics.SpriteEffects.FlipHorizontally;
                                player.feetBody.LinearVelocity = new Vector2(-movementSpeed, player.feetBody.LinearVelocity.Y);
                            }
                            foreach (BasePlayer player in playerGroupA)
                            {
                                player.desiredSpriteEffect = Microsoft.Xna.Framework.Graphics.SpriteEffects.None;
                                player.feetBody.LinearVelocity = new Vector2(movementSpeed, player.feetBody.LinearVelocity.Y);
                            }
                        }
                        else
                        {
                            if (splitting)
                            {
                                foreach (BasePlayer player in players)
                                {
                                    player.feetBody.LinearVelocity = new Vector2(0, player.body.LinearVelocity.Y);
                                }
                                splitting = false;
                            }
                        }
                    }
                }
                previouskeyboardstate = currentKeyBoardState;
            }
        }
        public void resetLevel()
        {
            maingame.switchLevel(maingame.currentLevel);
            maingame.hud.candyCount = 0;
        }
        public static BasePlayer getPlayerWithID(int ID)
        {
            foreach (BasePlayer player in players)
            {
                if (player.ID == ID) return player;
            }
            return null;
        }

    }

}
